
local ServerTime={}
local _delta_t=0      --本地时间与服务器时间差
local ServerTimeZone=8*3600 	--服务器时区

-- 计算本地时区
local function get_timezone()
	local now = os.time()
	local utc_now=os.time(os.date("!*t", now))
	return os.difftime(now, utc_now)
end

local localTimeZone = get_timezone() 			--本地时区
local timeZoneD = ServerTimeZone - localTimeZone  --计算出服务端时区与客户端时区差值
    -- local CurrentDateTime = os.date("*t",ServerTimeStamp + timeZoneD)

---
--      设置服务器时间
--@param tm number 时间戳
function ServerTime.setTime(tm)
	_delta_t=tm-os.time()
end

---      
--      取此刻的服务器时间
--@return #number 当前时刻服务器的时间戳
function ServerTime.getTime()
	return os.time()+_delta_t
end

---
--		取当前服务器时间的日期
--@return #table os.date("*t")返回值
function ServerTime.getDate()
	local temp_time = ServerTime.getTime() + timeZoneD
	return os.date("*t",temp_time)
end

---
--     取时区差
function ServerTime.getTimeZoneDelta()
	return timeZoneD
end

---
--      在server_t时刻执行方法
--@param server_t number :服务器时间的时间戳（必须必当前时间大，否则立即执行)
--@param fun function :到时间后回调方法
function ServerTime.doAtTime(server_t,fun)
    scheduler.performWithDelayGlobal(fun,server_t-ServerTime.getTime())
end

---
--今天星期几(星期天是7)
function ServerTime.dayOfToday()
	local wday=os.date("%w",ServerTime.getTime()+timeZoneD)
	if wday==0 or wday=="0" then
		wday=7
	end
	--print("星期"..wday)
    return tonumber(wday)
end

--指定下个星期几的凌晨时间
--@param week int 指定的星期几
function ServerTime.nextWeekDayTimes(week)
	local wday = ServerTime.dayOfToday()
	local twelveHours = 12*3600	
	local curdate = os.date("*t",ServerTime.getTime()+timeZoneD)
    local sameday_zero = os.time{year=curdate.year, month=curdate.month, day=curdate.day} --今日凌晨
   	local times = (7-wday)+week-1		--距离指定的下个星期几的天数
   	local nextday_zero_hours = sameday_zero+twelveHours-ServerTime.getTime()	--当前时间距离次日凌晨的hours

   	return times*twelveHours*2+nextday_zero_hours
end
---
--现在是几点[0-23]
function ServerTime.hourOfNow()
	local hour=os.date("%H",ServerTime.getTime()+timeZoneD)
	--print(hour,"点")
	return tonumber(hour)
end


---
--几月份[1-12]
function ServerTime.mouthOfYear()
	local m=os.date("%m",ServerTime.getTime()+timeZoneD)
	print(m,"月")
	return tonumber(m)
end

---讲秒数转化为HH:MM:SS的格式
function ServerTime.secondToHHMMSS(second)
	local hou=math.floor(second/3600)
	local mus=math.floor((second-hou*3600)/60)
	local sec=second-hou*3600-mus*60
	return string.format("%02d:%02d:%02d",hou,mus,sec)
end

-- YYYY.MM.DD-HH:MM
function ServerTime.formatDateTo_YYYY_MM_DD_HH_MM( date )
	local sdate=date
    local str1=string.format("%04d.%02d.%02d",sdate.year,sdate.month,sdate.day)
    local str2=string.format("%02d:%02d",sdate.hour,sdate.min)
    return str1 .. "-" .. str2
end

function ServerTime.reset()
	local temp_time = ServerTime.getTime() + timeZoneD + 24*3600
	local date = os.date("*t",temp_time)
	date.hour=0
	date.min=0
	date.sec=0
	local targetT=os.time(date) - timeZoneD
	local deltaT=targetT-ServerTime.getTime()
	local handle
	local function onDayChange( delta )
		GlobalSchedulerHelper.removeOneHandler(handle)
		ServerTime.updateDay()
		handle=scheduler.performWithDelayGlobal(onDayChange, 24*3600)
		GlobalSchedulerHelper.addOneHandler(handle)
	end
	handle=scheduler.performWithDelayGlobal(onDayChange, deltaT)
	GlobalSchedulerHelper.addOneHandler(handle)
end

return ServerTime